#region using

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using PS.WebParts.Rollup.Utils;

#endregion

namespace PS.WebParts.Rollup.GridViewWebPart
{
    public class PSGridView
        : SPGridView
    {
        private string callbackFilterDataFieldName;
        private string callbackFilterResults;
        private string eventArgument;

        #region private class DataBoundHelper

        private class DataBoundHelper
            : SPBoundField
        {
            public static string GetValue(object dataItem, string dataFieldName)
            {
                return new DataBoundHelper().GetPropertyValue(dataItem, dataFieldName);
            }
        }

        #endregion

        protected override void RaiseCallbackEvent(string argument)
        {
            base.RaiseCallbackEvent(argument);
            eventArgument = argument;
        }

        protected override void OnLoad(EventArgs args)
        {
            base.OnLoad(args);
            ScriptLink.Register(Page, "PS_spgridview.js", false);
        }

        protected override string GetCallbackResult()
        {
            string valueAfterPrefix = StringUtil.GetValueAfterPrefix(eventArgument, "__SPGridView__;");

            if (null != valueAfterPrefix)
            {
                callbackFilterDataFieldName = valueAfterPrefix;
                if (string.IsNullOrEmpty(callbackFilterDataFieldName))
                {
                    return "SPGridView's RaiseCallbackEvent method was passed an empty data field name.";
                }
            }
            else
            {
                base.RaiseCallbackEvent(eventArgument);
                return base.GetCallbackResult();
            }

            DataSourceView data = GetData();
            DataSourceSelectArguments arguments = new DataSourceSelectArguments();
            arguments.MaximumRows = 0;
            arguments.RetrieveTotalRowCount = false;
            arguments.StartRowIndex = 0;
            callbackFilterResults = null;
            data.Select(arguments, new DataSourceViewSelectCallback(OnDataSourceViewSelectCallback));

            return callbackFilterResults;
        }

        private BoundField FindColumnByDataField(string fieldName)
        {
            BoundField res = null;
            foreach (DataControlField col in Columns)
            {
                BoundField bF = col as BoundField;
                if ((null != bF) && (fieldName == bF.DataField))
                {
                    res = bF;
                    break;
                }
            }
            return res;
        }

        private void OnDataSourceViewSelectCallback(System.Collections.IEnumerable data)
        {
            SortedList<string, string> list = new SortedList<string, string>();

            System.Collections.IEnumerator enumerator = data.GetEnumerator();
            while (true)
            {
                string str;
                do
                {
                    if (!enumerator.MoveNext())
                    {
                        StringBuilder builder = new StringBuilder();
                        using (IEnumerator<KeyValuePair<string, string>> enumerator2 = list.GetEnumerator())
                        {
                            KeyValuePair<string, string> pair;
                        Label_0011:
                            if (enumerator2.MoveNext())
                            {
                                goto Label_002B;
                            }
                            callbackFilterResults = builder.ToString();
                            return;
                        Label_001C:
                            builder.Append(";");
                            goto Label_0040;
                        Label_002B:
                            pair = enumerator2.Current;
                            if (builder.Length > 0)
                            {
                                goto Label_001C;
                            }
                        Label_0040:
                            builder.Append(pair.Value.Replace("%", "%25").Replace(";", "%3b"));
                            goto Label_0011;
                        }
                    }
                    object current = enumerator.Current;
                    DataBinder.GetPropertyValue(current, callbackFilterDataFieldName);

                    str = DataBoundHelper.GetValue(current, callbackFilterDataFieldName);

                    BoundField bF = FindColumnByDataField(callbackFilterDataFieldName);
                    if (bF != null)
                    {
                        FormattedColumn fC = bF as FormattedColumn;
                        if (fC != null)
                        {
                            str += "##" + fC.GetDisplayString(current);
                        }
                    }
                }
                while (list.ContainsKey(str));
                list.Add(str, str);
            }
        }
    }
}
